% This file compares exposures measures to the China shock

clear all
clc

% Get results in Flexible case

load('Results1Flex')
LDCfa                = LHCfa .* LDBas;
LLBas                = lLBas;
LLBas(:,2:end,2:end) = repmat(lLBas(:,2:end,1),1,1,T-1).*cumprod(LDBas,3);
LLCfa                = lLCfa;
LLCfa(:,2:end,2:end) = repmat(lLCfa(:,2:end,1),1,1,T-1).*cumprod(LDCfa,3);
lShaReg              = lLBas(:,:,2)./repmat(sum(lLBas(:,:,2),2),1,Splus);
AggWelf              = sum(WELF .* lShaReg,2)*100;
AggWelfU             = AggWelf(1:M);
TotalEmploymentB     = squeeze(sum(LLBas(1:M,2:end,:),2));
TotalEmploymentC     = squeeze(sum(LLCfa(1:M,2:end,:),2));
lab1                 = (TotalEmploymentC(:,8)-TotalEmploymentB(:,8))./TotalEmploymentB(:,1)*100;
wel1                 = AggWelfU;
clearvars -except lab1 wel1

% Get results in DNWR case

load('Results1')
LDCfa                = LHCfa .* LDBas;
LLBas                = lLBas;
LLBas(:,2:end,2:end) = repmat(lLBas(:,2:end,1),1,1,T-1).*cumprod(LDBas,3);
LLCfa                = lLCfa;
LLCfa(:,2:end,2:end) = repmat(lLCfa(:,2:end,1),1,1,T-1).*cumprod(LDCfa,3);
lShaReg              = lLBas(:,:,2)./repmat(sum(lLBas(:,:,2),2),1,Splus);
AggWelf              = sum(WELF .* lShaReg,2)*100;
AggWelfU             = AggWelf(1:M);
TotalEmploymentB     = squeeze(sum(LLBas(1:M,2:end,:),2));
TotalEmploymentC     = squeeze(sum(LLCfa(1:M,2:end,:),2));
lab2                 = (TotalEmploymentC(:,8)-TotalEmploymentB(:,8))./TotalEmploymentB(:,1)*100;
wel2                 = AggWelfU;

% Define exposure measures

bartikjose           = xlsread('Inputs/InputData.xlsx','EXP','B2:B51');
barr                 = bartikjose*2.63/mean(bartikjose);
sharesfor2           = (revbysec-expbysec)./repmat(sum(expbysec,2),1,S);
bar8                 = sharesfor2(1:50,:)*changeseccha;
atrans               = std(barr)/std(bar8);
btrans               = mean(barr)-atrans*mean(bar8);
bar8renorm           = atrans*bar8+btrans;

% Now run horse races between ADH exposure and net export exposure in case
% with and without DNWR for both welfare and labor

% Table D.1, column 1

hr5                  = fitlm([barr,bar8renorm],wel1);
Results([1,3,5],1)   = hr5.Coefficients.Estimate;
Results([2,4,6],1)   = hr5.Coefficients.SE;
Results(7,1)         = M;
Results(8,1)         = hr5.Rsquared.Ordinary;
Results(9,1)         = mean(wel1);

% Table D.1, column 2

hr6                  = fitlm([barr,bar8renorm],wel2);
Results([1,3,5],2)   = hr6.Coefficients.Estimate;
Results([2,4,6],2)   = hr6.Coefficients.SE;
Results(7,2)         = M;
Results(8,2)         = hr6.Rsquared.Ordinary;
Results(9,2)         = mean(wel2);

% Table D.1, column 3

hr7                  = fitlm([barr,bar8renorm],lab1);
Results([1,3,5],3)   = hr7.Coefficients.Estimate;
Results([2,4,6],3)   = hr7.Coefficients.SE;
Results(7,3)         = M;
Results(8,3)         = hr7.Rsquared.Ordinary;
Results(9,3)         = mean(lab1);

% Table D.1, column 1

hr8                  = fitlm([barr,bar8renorm],lab2);
Results([1,3,5],4)   = hr8.Coefficients.Estimate;
Results([2,4,6],4)   = hr8.Coefficients.SE;
Results(7,4)         = M;
Results(8,4)         = hr8.Rsquared.Ordinary;
Results(9,4)         = mean(lab2);

% Display Results

disp(compose('%.3f',Results))
